import { ref } from "vue"
export function useClock(useCommonState){
	
	const changeClockType = (item) => {
		  useCommonState.current = item
	}
	const changeClockMode = (item) => {
		  useCommonState.mode = item
	}
	
  const getRandomNumber = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }
  // 角度计算
  const calculateAngles = (h, m) => {
    // 计算时针的角度
    let hourAngle = 30 * h + 0.5 * m;
    // 计算分针的角度
    let minuteAngle = 6 * m;

    return {
        hourAngle: hourAngle,
        minuteAngle: minuteAngle
    };
  }

  // 上午
  const useMorning = () => {
      useCommonState.topicList.push(morning(0, 12))
  }

  // 下午
  const useAfternoon = () => {
      useCommonState.topicList.push(morning(12, 24))
  }

  // 全天
  const useAllday = () => {
      const idx = getRandomNumber(0, 2) + 1
      if(idx === 1) { useCommonState.topicList.push(morning(0, 12)) }
      if(idx === 2) { useCommonState.topicList.push(morning(12, 24)) }
      if(idx === 3) { useCommonState.topicList.push(morning(0, 24)) }
  }
  
  // 半时
  const modeHalf = () => {
	  return 30
  }
  // 整时
  const modeHour = () => {
  	  return '00'
  }
  // 整分
  const modeMinute = () => {
  	  const minuteArr = ['00', 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]
  	  const idx = getRandomNumber(0, minuteArr.length - 1)
  	  console.log('idx', idx)
  	  return minuteArr[idx]
  }

  const morning = (start, end) => {
    let hour = getRandomNumber(start, end);
    let minute = null
	// 时间模式
	if(useCommonState.mode) {
		if(useCommonState.mode.half) {
			minute = modeHalf()
		}
		if(useCommonState.mode.hour) {
			minute = modeHour()
		}
		if(useCommonState.mode.minute) {
			minute = modeMinute()
		}
		if(useCommonState.mode.blend) {
			const idx = getRandomNumber(1, 3)
			if(idx === 1) { minute = modeHalf() }
			if(idx === 2) { minute = modeHour() }
			if(idx === 3) { minute = modeMinute() }
		}
	}else{
		minute = getRandomNumber(0, 60)
	}
	console.log(111, hour, minute)
    const Angles = calculateAngles(hour, minute)
	if(minute !== '00' && minute < 10) {
		minute = `0${minute}`
	}
	if(hour == 0) {
		hour = 12
	}
    return {
		hour, 
		minute, 
		Angles, 
		input: [],
		result: `${hour},${minute}`,
		validate: false
	}
  }
  
 
  // 创建
  const useCreateClock = () => {
  	return new Promise((resolve, reject) => {
  		if(!useCommonState.topicNumber) {
  			  wx.showToast({title:"请选择数量", icon: "none"})
  			  reject()
  			  return
  		}
  		// if(!useCommonState.current) {
  		// 	  wx.showToast({title:"请选择时间模式", icon: "none"})
  		// 	  reject()
  		// 	  return
  		// }
  		useCommonState.topicList = []
		for(let i = 0; i < useCommonState.topicNumber; i++){
			useMorning()
		}
  		 // if(useCommonState.current.morning) {
  			// for(let i = 0; i < useCommonState.topicNumber; i++){
  			// 	useMorning()
  			// }
  		 // }
  		 
  		 // if(useCommonState.current.afternoon) {
  			// for(let i = 0; i < useCommonState.topicNumber; i++){
  			// 	useAfternoon()
  			// }
  		 // }
  		 
  		 // if(useCommonState.current.allday) {
  			// for(let i = 0; i < useCommonState.topicNumber; i++){
  			// 	useAllday()
  			// }
  		 // }
  		 resolve()
  	 })
  }

  return {
	  useCreateClock,
	  changeClockType,
	  changeClockMode
  }

}